package com.eastfair.certificate.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.eastfair.certificate.vo.FormFieldSubsystemVO;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.utils.MapFlatUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Date:2022/7/25
 * Time:13:54
 *
 * @author ligang
 */
@Slf4j
public class ConvertByFormField {

    /**
     * field转object
     *
     * @param formFieldSubsystemVOList
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> R<T> assembleFieldToObject(List<FormFieldSubsystemVO> formFieldSubsystemVOList, Class<T> clazz) {
        if (CollectionUtil.isEmpty(formFieldSubsystemVOList)) {
            return R.success(null);
        }
        Map<String, Object> entityMap = new HashMap<>();
        formFieldSubsystemVOList.parallelStream().forEach(formFieldVO -> {
            //暂时只处理keyword
            entityMap.put(formFieldVO.getKeyword(), formFieldVO.getValue());
            //处理前缀
        });
        return R.success((T) MapFlatUtil.mapToFlatBean(entityMap, clazz));
    }

    public static void main(String[] args) {
        Date date = null;
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm");
//        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.parse("2022-08-22T11:11:11", formatter), LocalTime.MIN);
        String strDate2 = "2019-12-01T17:02:30";
        LocalDateTime ldt = DateUtil.parseLocalDateTime(strDate2, DatePattern.NORM_DATETIME_MS_PATTERN);
        String strDate1 = DateUtil.format(ldt, DatePattern.NORM_DATETIME_PATTERN);

        try {
            Date date1= DatePattern.UTC_SIMPLE_FORMAT.parse(strDate2);
            System.out.println(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(strDate1);
//        DatePattern.UTC_SIMPLE_FORMAT;
//        DatePattern.UTC_PATTERN
//        try {
//            date = DateUtils.parseDate("2022-08-22T11:11:11","YYYY-MM-ddTHH:mm:ss");
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        CertificateUserDTO certificateUserDTO = new CertificateUserDTO();
//        certificateUserDTO.setComeInEndTime(LocalDateTime.now());
//        certificateUserDTO.setBooth("33333");
//        List<FormFieldSubsystemVO> formFieldSubsystemVOList = new ArrayList<>();
//        FormFieldSubsystemVO formFieldSubsystemVO = new FormFieldSubsystemVO();
//        formFieldSubsystemVO.setKeyword("booth");
//        formFieldSubsystemVOList.add(formFieldSubsystemVO);
//        formFieldSubsystemVO = new FormFieldSubsystemVO();
//        formFieldSubsystemVO.setKeyword("comeInEndTime");
//        formFieldSubsystemVOList.add(formFieldSubsystemVO);
//        R<List<FormFieldSubsystemVO>> r = assembleObjectToField(formFieldSubsystemVOList, certificateUserDTO);
        System.out.println(date);
    }

    /**
     * object转field
     *
     * @param formFieldSubsystemVOList
     * @param object
     * @param <T>
     * @return
     */
    public static <T> R<List<FormFieldSubsystemVO>> assembleObjectToField(List<FormFieldSubsystemVO> formFieldSubsystemVOList, Object object) {
        if (CollectionUtil.isEmpty(formFieldSubsystemVOList)) {
            return R.success(null);
        }
        // 对象扁平化处理z
        Map<String, Object> objectMap = MapFlatUtil.beanToFlatMap(object);
        for (FormFieldSubsystemVO formFieldSubsystemVO : formFieldSubsystemVOList) {
            for (String key : objectMap.keySet()) {
                if (formFieldSubsystemVO.getKeyword().equals(key)) {
                    Object object1 = objectMap.get(key);
                    LocalDateTime localDateTime = null;
                    try {
                        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        localDateTime = LocalDateTime.parse(object1.toString(), df);
                        formFieldSubsystemVO.setValue(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    } catch (Exception e) {
//                        log.info("不是时间");
                    }
                    if (localDateTime == null) {
                        formFieldSubsystemVO.setValue(objectMap.get(key));
                    }

                }
            }
        }

        //穿插主键id或关联id
        for (String key : objectMap.keySet()) {
            if (key.equals("id") || (key.length() > 2 && key.substring(key.length() - 2).toLowerCase().indexOf("id") > -1)) {
                List<FormFieldSubsystemVO> formFieldSubsystemVOList1 = formFieldSubsystemVOList.stream().filter(s -> key.equals(s.getKeyword())).collect(Collectors.toList());
                if (formFieldSubsystemVOList1 == null || formFieldSubsystemVOList1.isEmpty()) {
                    FormFieldSubsystemVO formFieldSubsystemVO = new FormFieldSubsystemVO();
                    formFieldSubsystemVO.setValue(objectMap.get(key));
                    formFieldSubsystemVO.setIsExtraParam(BusinessConstant.YES);
                    formFieldSubsystemVO.setKeyword(key);
                    formFieldSubsystemVOList.add(formFieldSubsystemVO);
                }
            }
        }
        return (R<List<FormFieldSubsystemVO>>) R.success(formFieldSubsystemVOList);
    }

}
